﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;

namespace imdoo.svr
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class UserInfo
    {
        /// <summary>
        /// 编码
        /// </summary>
        public string IDCode { get; set; }

        /// <summary>
        /// 名字
        /// </summary>
        public string Name { get; set; }
    }

    /// <summary>
    /// Summary description for vol
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class vol : System.Web.Services.WebService
    {
        static Dictionary<string, UserInfo> qqtoken = new Dictionary<string, UserInfo>();

        static Dictionary<string, string> authtoken = new Dictionary<string, string>();

        XElement GetRoot()
        {
            var xd = XDocument.Load(Server.MapPath("~/App_Data/data.xml"));
            var root = xd.Root;
            return root;
        }

        void Save(XElement root)
        {
            root.Document.Save(Server.MapPath("~/App_Data/data.xml"));
        }

        public class RequestBase
        {
            public string token { get; set; }
            public string auth { get; set; }

            public string IDCode
            {
                get
                {
                    return qqtoken[token].IDCode;
                }
            }
        }


        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        #region TryLogin

        /// <summary>
        /// 尝试用绑定的token登录【更新auth】
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [WebMethod]
        public object TryLogin(string token)
        {
            if (qqtoken.ContainsKey(token))
            {
                string auth = Guid.NewGuid().ToString();

                if (!authtoken.ContainsKey(token)) authtoken.Add(token, auth);
                else authtoken[token] = auth;

                return new { User = qqtoken[token], Auth = auth };
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region Reg

        public class RegRequest
        {
            public string token { get; set; }
            public string Name { get; set; }
        }

        /// <summary>
        /// 尝试注册
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [WebMethod]
        public object Reg(RegRequest request)
        {
            if (qqtoken.ContainsKey(request.token))
            {
                throw new Exception("错误，该用户已经注册");
            }
            else
            {
                UserInfo ui = new UserInfo { IDCode = Guid.NewGuid().ToString(), Name = request.Name };
                qqtoken.Add(request.token, ui);
                string auth = Guid.NewGuid().ToString();
                authtoken.Add(request.token, auth);
                return new { User = ui, Auth = auth };
            }
        }

        #endregion

        #region CreateUpdateAct

        public class UpdateActRequest : RequestBase
        {
            /// <summary>
            /// 活动名称
            /// </summary>
            public string ActName { get; set; }
            /// <summary>
            /// 活动开展日期（预计）
            /// </summary>
            public DateTime ActDate { get; set; }
            /// <summary>
            /// 活动描述
            /// </summary>
            public string ActDesc { get; set; }
        }

        [WebMethod]
        public object UpdateAct(UpdateActRequest request)
        {
            var root = GetRoot();
            var newdata = new XElement("Act") { Value = request.ActDate.ToString() };
            newdata.SetAttributeValue("Name", request.ActName);
            newdata.SetAttributeValue("Creator", request.IDCode);
            root.Add(newdata);
            Save(root);
            return true;
        }

        #endregion

        #region GetActs

        public class ActItem
        {
            public string ActName { get; set; }
            public string ActDate { get; set; }
        }

        [WebMethod]
        public ActItem[] GetActs(RequestBase request)
        {
            var root = GetRoot();
            var data = root.Elements().Select(r => new ActItem { ActName = r.Attribute("Name").Value, ActDate = r.Value }).ToArray();
            return data;
        }

        #endregion

        [WebMethod]
        public ActItem[] GetMyActs(RequestBase request)
        {
            string idcode = request.IDCode;
            var root = GetRoot();
            var data = root.Elements().Where(r => r.Attribute("Creator").Value == idcode)
                .Select(r => new ActItem { ActName = r.Attribute("Name").Value, ActDate = r.Value }).ToArray();
            return data;
        }
    }
}
